﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FullThrustLogic.Core;
using FullThrustLogic.Core.GameState;
using FullThrustLogic.ShipComponent;
using FullThrustLogic.ShipComponent.Weapon;
using LogicTest.Controls;

namespace LogicTest.Forms
{
    public partial class AssignFCSWizard : Form
    {
        private Ship SelectedShip;
        private GameContext GameContext;

        public AssignFCSWizard(Ship selectedShip,  GameContext gameContext)
        {
            InitializeComponent();
            GameContext = gameContext;
            SelectedShip = selectedShip;
        }

        private void AssignFCSWizard_Load(object sender, EventArgs e)
        {
            tabControl1.TabPages.Clear();

            foreach (FireControlSystem fcs in SelectedShip.FireControlSystems)
            {
                if (fcs.Enabled)
                {
                    TabPage fcsTabPage = new TabPage("FCS");
                    FireControlAssignment fireControlAssignment = new FireControlAssignment(SelectedShip, fcs, GameContext);

                    fireControlAssignment.Name = "FireControlAssignment";
                    fireControlAssignment.Dock = DockStyle.Fill;

                    fcsTabPage.Controls.Add(fireControlAssignment);

                    tabControl1.TabPages.Add(fcsTabPage);
                    
                }
            }
        }

        private void BtnOkay_Click(object sender, EventArgs e)
        {
            StoreAttackOrders(false);

            this.Close();
        }

        private void StoreAttackOrders(bool clearAttackOrders)
        {
            foreach (TabPage tp in tabControl1.TabPages)
            {
                FireControlAssignment fca = (FireControlAssignment)tp.Controls.Find("FireControlAssignment", true)[0];
                if (fca.cbShipsInRange.SelectedItem is Ship)
                {
                    foreach (ListViewItem lvi in fca.lvWeapons.CheckedItems)
                    {
                        if (clearAttackOrders)
                        {
                            fca.SelectedFCS.AttackOrders.Clear();
                        }

                        fca.SelectedFCS.TargetShip = (Ship)fca.cbShipsInRange.SelectedItem;

                        if (lvi.Tag is iWeapon)
                        {
                            fca.SelectedFCS.AttackOrders.Add(new AttackOrder() { TargetingWeapon = (iWeapon)lvi.Tag });
                        }
                    }
                }
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            StoreAttackOrders(false);

            if (tabControl1.SelectedTab != null)
            {
                FireControlAssignment fca = (FireControlAssignment) tabControl1.SelectedTab.Controls.Find("FireControlAssignment", true)[0];
                fca.LoadAvailableWeapons();
            }
        }


    }
}
